using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D Rb;
    public SpriteRenderer Model;
    public KeyCode LeftKey;
    public KeyCode RightKey;
    public KeyCode JumpKey;
    public KeyCode DiveKey;
    public bool InWater;

    //public float ForceMultiplier = 5.0f;
    //public float DampingMultiplier = 1.0f;

    //public float ForceGap = 1000.0f;

    public float MoveForce = 1000.0f;
    public float JumpForce = 1000.0f;
    public float DiveForce = 1000.0f;

    // Start is called before the first frame update
    void Start()
    {
        Rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (InWater && Input.GetKeyDown(JumpKey))
        {
            Rb.AddForce(Vector2.up * JumpForce);
        }

        if (InWater && Input.GetKeyDown(DiveKey))
        {
            Rb.AddForce(Vector2.down * DiveForce);
        }

        if (Rb.position.x < GameManager.Instance.LeftWall)
        {
            var newPos = Rb.position;
            newPos.x = GameManager.Instance.LeftWall;
            Rb.MovePosition(newPos);

            if (Rb.velocity.x < 0.0f)
            {
                var newVelocity = Rb.velocity;
                newVelocity.x = 0.0f;
                Rb.velocity = newVelocity;
            }
        }

        if (Rb.position.x > GameManager.Instance.RightWall)
        {
            var newPos = Rb.position;
            newPos.x = GameManager.Instance.RightWall;
            Rb.MovePosition(newPos);

            if (Rb.velocity.x > 0.0f)
            {
                var newVelocity = Rb.velocity;
                newVelocity.x = 0.0f;
                Rb.velocity = newVelocity;
            }
        }
    }

    void FixedUpdate()
    {
        bool bKeyDownLeft = Input.GetKey(LeftKey);
        bool bKeyDownRight = Input.GetKey(RightKey);

        // 都按下了，那就啥都不干
        //if (bKeyDownLeft && bKeyDownRight)
        //{
        //    return;
        //}
        if (bKeyDownLeft || bKeyDownRight)
        {
            Model.flipX = bKeyDownLeft;
        }

        if (bKeyDownLeft)
        {
            Rb.AddForce(Vector2.left * MoveForce);
        }

        if (bKeyDownRight)
        {
            Rb.AddForce(Vector2.right * MoveForce);
        }

        //Vector3 Postion = Rb.transform.position;
        //if (XGap <= Mathf.Abs(Postion.x) && 0.0f != CurForce)
        //{
        //    Postion.x = Mathf.Clamp(Postion.x, -XGap, XGap);
        //    Rb.transform.position = Postion;
        //    Rb.AddForce(Vector2.left * CurForce);
        //    CurForce = 0.0f;
        //    return;
        //}

        //float Res = 0.0f;
        //if (bKeyDownLeft)
        //{
        //    Res = Mathf.Max(CurForce - ForceMultiplier, -ForceGap);

        //}
        //else if (bKeyDownRight)
        //{
        //    Res = Mathf.Min(CurForce + ForceMultiplier, ForceGap);
        //}
        //else if (0.0f == CurForce)
        //{
        //    return;
        //}
        //else
        //{
        //    Res = CurForce > 0.0f ? Mathf.Max(CurForce - DampingMultiplier* ForceMultiplier, -CurForce) : Mathf.Min(CurForce + DampingMultiplier * ForceMultiplier, -CurForce);
        //}

        //float DeltaForce = Res - CurForce;
        //if (0.0f == DeltaForce)
        //    return;

        //CurForce = Res;
        //Rb.AddForce(Vector2.right * DeltaForce);
    }

    public void SetWaterState(bool newState)
    {
        if (InWater == newState)
            return;

        InWater = newState;
    }
}
